package net.cathackers.devel.scmp.chat.impl.xmpp.presence;

import org.apache.log4j.Logger;

import net.cathackers.devel.scmp.chat.factories.ChatServiceFactory;
import net.cathackers.devel.scmp.chat.impl.groupchat.GroupChatManagerImpl;
import net.cathackers.devel.scmp.chat.impl.xmpp.iqcomponent.interfaces.XMPPComponent;
import net.cathackers.devel.scmp.chat.interfaces.GroupChatManager;

import net.cathackers.devel.scmp.factories.SCMPFactory;

import net.cathackers.devel.scmp.xmpp.Packet;
import net.cathackers.devel.scmp.xmpp.PresencePacket;
import net.cathackers.devel.scmp.xmpp.PresencePacket.PresenceType;
/**
 * PresenceComponent gets the request of the user who wants to join a group chat
 * then sends that request to be handled by GroupChatManagerImpl
 * it receives also, presencePacket that shows the status of the user to the other 
 * members then routes it to PresenceHandler to be handled according to its type
 * 
 */
public class PresenceComponent extends XMPPComponent<Packet> {
    /**
     * presenceComponent has handler_ attribute which is a PresenceHandler
     * it's used to route the user presence according to its type to be handled 
     * by PresenceHandler
     * manager is an object of GroupChatManagerImpl which is used to
     * handle the request of the user to join group chat
     * groupChatManager_ is used to handle the presence of that user
     */

    private PresenceHandler handler_;
    private Logger logger_;
    private GroupChatManager groupChatManager_;
   
    GroupChatManagerImpl manager = new GroupChatManagerImpl();
    /**
     * Constructs new PresenceComponent object
     * that will be handled by PresenceHandler
     * @param h
     * 
     * 
     */

    public PresenceComponent(PresenceHandler h) {
	handler_ = h;
	logger_ = SCMPFactory.getLogger(this);
	this.groupChatManager_ = ChatServiceFactory.getGroupchatManager();
    }

    @Override
   /**
    * this method determines the presence type of the user
    * @param t a packet that contains user's data such as UserID
    * when user  wants to join a group chat his presence has to be declared 
    * PresecePacket type  shows the status of the user
    * presence will be handled according to presence type 
     */
    
    
    public void process(Packet t) {

	PresencePacket presence = (PresencePacket) t;
	PresenceType type = presence.getPresenceType();

	if (manager.isGroupChatRequest(presence)) {
            logger_.debug("the PresenceComponent received a presence related to GroupChat  ");
            groupChatManager_.handlePresence(presence);

	}
	logger_.debug("the PresenceComponent received a presence of type  "
		+ type);

	switch (type) {
	case AVAILABLE:
	    //User is able to receive messages
	    handler_.handleAvailablePresence(presence);
	    break;
	case UNAVAILABLE:
	    // TODO: remove this user from his contacts...
	    if (null == presence.getTo()) {
		handler_.broadcastPresence(presence);
	    } else {
		handler_.forwardPresence(presence);
	    }
	    break;
	case SUBSCRIBE:
	    //Request a subscription to a user’s presence.
	    handler_.handleSubscribePresence(presence);
	    break;
	case UNSUBSCRIBE:
	    //Request removal of an existing presence subscription
	    handler_.handleUnSubscribePresence(presence);
	    break;
	case SUBSCRIBED:
	    //Subscription to the sender’s presence has been accepted
	    

	    handler_.handleSubscribedPresence(presence);
	    break;
	case UNSUBSCRIBED:
	    //Subscription to the sender’s presence has been removed
	    handler_.handleUnSubscribedPresence(presence);
	    break;
	case PROBE:
	    //  Sends all presence information from one server to another (servers only)
	     
	    

	    // TODO we will implement it later
	    break;
	case ERROR:
	    //The standard Jabber Error packet for presence problems
	    handler_.forwardPresence(presence);
	}

    }

}
